{% extends 'base.html' %}

{% block title %}碳盘查管理{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>碳盘查管理</h1>
    <a href="{{ url_for('inventory.add_inventory') }}" class="btn btn-primary">添加盘查事项</a>
</div>

<div class="card mb-4">
    <div class="card-body">
        <form method="get" action="{{ url_for('inventory.inventory_list') }}" class="d-flex">
            <input type="text" name="search" class="form-control me-2" placeholder="搜索名称、模型分类、碳盘查目录或基准年..." value="{{ search_query }}">
            <button type="submit" class="btn btn-outline-primary">搜索</button>
            {% if search_query %}
                <a href="{{ url_for('inventory.inventory_list') }}" class="btn btn-outline-secondary ms-2">清除</a>
            {% endif %}
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover table-sm text-nowrap">
                <thead class="table-light sticky-top">
                    <tr>
                        <th class="sortable" data-sort="name">名称 <i class="bi bi-sort"></i></th>
                        <th class="d-none d-md-table-cell">模型分类</th>
                        <th class="d-none d-lg-table-cell">碳盘查目录</th>
                        <th class="d-none d-xl-table-cell">组织边界</th>
                        <th class="d-none d-md-table-cell">基准年</th>
                        <th class="d-none d-lg-table-cell">核算标准</th>
                        <th class="d-none d-xl-table-cell">核算方式</th>
                        <th>所属年份</th>
                        <th>所属月份</th>
                        <th>最后修改人</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% if inventories|length == 0 %}
                    <tr>
                        <td colspan="11" class="text-center py-4">
                            <div class="text-muted">
                                <i class="bi bi-folder-x fs-3"></i>
                                <p class="mt-2">暂无碳盘查记录</p>
                                <a href="{{ url_for('inventory.add_inventory') }}" class="btn btn-sm btn-primary mt-1">添加记录</a>
                            </div>
                        </td>
                    </tr>
                    {% else %}
                        {% for inventory in inventories %}
                        <tr>
                            <td>{{ inventory.name }}</td>
                            <td class="d-none d-md-table-cell">{{ inventory.model_category_name }}</td>
                            <td class="d-none d-lg-table-cell">{{ inventory.inventory_category_name }}</td>
                            <td class="d-none d-xl-table-cell">{{ inventory.organization_boundary }}</td>
                            <td class="d-none d-md-table-cell">{{ inventory.base_year }}</td>
                            <td class="d-none d-lg-table-cell">{{ inventory.accounting_standard }}</td>
                            <td class="d-none d-xl-table-cell">{{ inventory.accounting_method }}</td>
                            <td>{{ inventory.belong_year }}</td>
                            <td>{{ inventory.belong_month }}</td>
                            <td>{{ inventory.last_modified_by }}</td>
                            <td><span class="badge {{ status_badge_class(inventory.status) }}">{{ inventory.status }}</span></td>
                            <td>
                                <div class="btn-group btn-group-sm">
                                    <!-- <a href="{{ url_for('inventory.inventory_detail', id=inventory.id) }}" class="btn btn-outline-primary">详情</a> -->
                                    <a href="{{ url_for('inventory.edit', id=inventory.id) }}" class="btn btn-outline-secondary">编辑</a>
                                    <button type="button" class="btn btn-outline-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ inventory.id }}" onclick="event.stopPropagation();">删除</button>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    {% endif %}
                </tbody>
            </table>
        </div>
    </div>
</div>

{# 将所有模态框移到表格外部 #}
{% for inventory in inventories %}
<div class="modal fade" id="deleteModal{{ inventory.id }}" tabindex="-1">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除 <strong>{{ inventory.name }}</strong> 吗？</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">取消</button>
                <form action="{{ url_for('inventory.delete_inventory', id=inventory.id) }}" method="post">
                    <button type="submit" class="btn btn-sm btn-danger">删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endfor %}

{% endblock %}